<?php 
	//require_once("function/function.php");
	//require_once("function/database.php");
	//$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
	//$objDB = mysql_select_db("db_stock"); 
	require_once("function/initialize.php");
	require_once("function/file.php");
	openConnection();
	if(isset($_REQUEST["logout"]))
		logout();
	if(!isLoggedIn())
		pageRedirect("login.php","");
	
		$sql_cart = "SELECT * FROM tbl_carts";
		$result_cart = mysql_query($sql_cart);
		$row_cart=fetch_assoc($result_cart);
		//check if cart empty
		if(isset($_GET['control']) && $_GET['control'] == "insert"){
				$salenumber=$_POST['salenumber'];
				$billnumber=$_POST['billnumber'];
				//select date n convert date
				$selected_date=$_POST['date'];
				$selected_date=strtotime($selected_date );
				$mysqldate = date( 'Y-m-d', $selected_date );
				$dateSale=$mysqldate;
				
				$customer=$_POST['customer'];
				if($customer==""){
					$customer="NaN";
				}
				$nameArray=$_POST['name'];
				$quantity=$_POST['quantity'];
				$brate=$_POST['brate'];
				$rate=$_POST['srate'];
				$avail=$_POST['avail'];
				$total=$_POST['total'];
				$payment=$_POST['payment'];
				$balance=$_POST['balance'];
				$subtotal=$_POST['subtotal'];		
				$mode=$_POST['mode'];
				//select date n convert date
				$selected_lastdate=$_POST['lastupdate'];
				$selected_lastdate=strtotime($selected_lastdate );
				$mysqllastdate = date( 'Y-m-d', $selected_lastdate );
				$lastupdate=$mysqllastdate;
				$leftMoney=$_POST['txtleftMoney'];
				$userName=getUserType();
				//$userName="Seller";
				$i=0;
				$j=1;
				foreach($nameArray as $name){
					$quantity=$_POST['quantity'][$i];
					$brate=$_POST['brate'][$i];
					$rate=$_POST['srate'][$i];
					$total=$_POST['total'][$i];
					
						//insert sell product
						$sql="INSERT INTO tbl_sales (sale_id,billnumber,sale_date,cus_name,pd_name,qty,buying_price,selling_price,total,paid,balance,amount,mode,
												user_name,sale_last_update,count)
						 VALUES('$salenumber','$billnumber','$dateSale','$customer','$name','$quantity','$brate','$rate','$total','$payment','$balance','$subtotal',
						 '$mode','$userName','$lastupdate','$j')";
						$result=query($sql);
						
						//select qty to update
						$sql="SELECT qty FROM  tbl_stocks_available WHERE pd_name='$name'";
						$result=query($sql);
						$Newqty=fetch_assoc($result);
						$oldqty=$Newqty['qty'];
						$Newqty=$oldqty-$quantity;
						
						//insert sell product
						$sql="INSERT INTO tbl_stocks (stock_id,pd_name,qty,selling_price,opening_stock,closing_stock,total,pd_date
							,user_name,billnumber,type,count)
							VALUES('$salenumber','$name','$quantity','$rate','$oldqty','$Newqty','$total','$dateSale','$userName',
							'$billnumber','sale','$j')";
						$result=query($sql);
						
						//update stock available
						$sql="UPDATE tbl_stocks_available SET qty='$Newqty' WHERE pd_name='$name'";
						$result=query($sql);	
					
					$i++;
					$j++;	
				}//end foreach	
				
				if($result){
					$sql_del="DELETE FROM tbl_carts";
					$result_del=query($sql_del);
					if($result_del){
						echo "<script>window.open('sales_print.php?salenumber=$salenumber&leftMoney=$leftMoney','myNewWinsr','width=620,height=800,toolbar=0,menubar=no,status=no,resizable=yes,location=no,directories=no');</script>";
						
						pageRedirect("index_user.php?page=sale_list&action=sales_user&","Successful...!");
					}
				}	
		}
closeConnection();
?>